﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;


namespace SQLServerDAL
{
    public class FilmTypeDAL : IFilmTypeDAL
    {
         public Boolean InsertFilmType(FilmType filmType)
        {
            try
            {
                
                FilmTypeDataClassesDataContext filmTypeContext = new FilmTypeDataClassesDataContext();
                filmTypeContext.FilmTypes.InsertOnSubmit(filmType);
                filmTypeContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean DeleteFilmType(int filmTypeId)
        {
            try
            {
                FilmTypeDataClassesDataContext filmTypeContext = new FilmTypeDataClassesDataContext();
                FilmType filmType = (from u in filmTypeContext.FilmTypes
                             where u.FilmTypeId.Equals(filmTypeId)
                             select u).First();
                filmTypeContext.FilmTypes.DeleteOnSubmit(filmType);
                filmTypeContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean UpdateFilmType(int filmTypeId, FilmType filmType)
        {

            if (filmTypeId != filmType.FilmTypeId)
            {
                return false;
            }
            FilmTypeDataClassesDataContext filmTypeContext = new FilmTypeDataClassesDataContext();
            FilmType query = (from u in filmTypeContext.FilmTypes
                          where u.FilmTypeId.Equals(filmTypeId)
                          select u).First();
            //query.Password = filmType.Password;

            filmTypeContext.SubmitChanges();
            return true;

        }

        public FilmType SelectFilmTypeById(int filmTypeId)
        {
            FilmTypeDataClassesDataContext filmTypeContext = new FilmTypeDataClassesDataContext();
            FilmType filmType = (from u in filmTypeContext.FilmTypes
                         where u.FilmTypeId.Equals(filmTypeId)
                         select u).First();
            return filmType;
        }


        public List<FilmType> SelectAllFilmType()
        {
            FilmTypeDataClassesDataContext filmTypeContext = new FilmTypeDataClassesDataContext();
            var query = (from u in filmTypeContext.FilmTypes
                         select u);
            List<FilmType> filmTypeList = new List<FilmType>();

            foreach (var filmType in query)
            {
                filmTypeList.Add(filmType);
            }
            return filmTypeList;
        }

    }
}
